	var myMedia = null;
	var mediaTimer = null;
	// duration of media (song)
	var dur = -1;
	var playing = false;
	
	var lastFlipVal = "off";
	
	//Flip control
	var accelWatch = null;
	var options = { 'frequency' : 300 };
	var lastZ = null;

	function updateAcceleration(a) {    
	    var changeThreshhold = 10;
	    
	    if (lastZ !== null) {  								// not first time
	        var deltaZ = Math.abs(lastZ - a.z);
	        if (deltaZ > changeThreshhold) { 
	            if (lastZ > 0) { playAudio(); }
	            else { myMedia.pause(); }          
	            lastZ = null;          
	            return;
	            } 
	    }
	    lastZ = a.z;
	}

	var setFlipper = function(state) {
	    if (state === "off") {
	        if (accelWatch) {
	            navigator.accelerometer.clearWatch(accelWatch);
	            accelWatch = null;
	        }
	    } else {
	        accelWatch = navigator.accelerometer.watchAcceleration(
	                updateAcceleration, 
	                function(ex) {
	                    alert("accel fail (" + ex.name + ": " + ex.message + ")");
	                }, 
	                options);
	    }    
	};
	//end flipcontrol

    function playAudio() {
		if (!playing) {
	        $("#media_dur").html(myMedia.getDuration());
	        $("#audio_position").html("Henter...");
                        myMedia.play();	
			document.getElementById('play').src = "images/pause.png";
			playing = true;	
		} else {
			myMedia.pause();
                        document.getElementById('play').src = "images/play.png";    
                        playing = false; 
		}
    }

    function stopAudio() {
		myMedia.stop();
		playing = false;
                document.getElementById('play').src = "images/play.png";    
		document.getElementById('audio_position').innerHTML = "0 Sek.";
		document.getElementById('media_dur').innerHTML = "0";
	}

   function onLoad() {
       document.addEventListener("deviceready", onDeviceReady, false);
   }
   
   function onDeviceReady(){
   	console.log("Got device ready");
   	updateMedia();
   }
   
   function updateMedia(src) {
   	    // Clean up old file
   	    if (myMedia != null) {
			myMedia.release();
		}

		// Get the new media file
		var yourSelect = document.getElementById('playlist');		
                myMedia = new Media(yourSelect.options[yourSelect.selectedIndex].value, stopAudio, null);
                $("#media_dur").html(myMedia.getDuration());
                
             // Update my_media position every second
                if (mediaTimer === null) {
                    mediaTimer = setInterval(function() {
                        myMedia.getCurrentPosition(
                                // success callback
                                function(position) {
                                    if (position > -1) {
                                        setAudioPosition(Math.round(position));
                                        // getDuration() can take a few seconds so keep trying
                                        // this could be done a better way, no callback for it
                                        if (dur <= 0) {
                                            dur = myMedia.getDuration();                             
                                            if (dur > 0) {
                                                dur = Math.round(dur);
                                                $("#media_dur").html(dur);
                                            }
                                        }                                                      
                                    }
                                },
                                // error callback
                                function(e) {
                                    alert("Error getting pos=" + e);
                                    setAudioPosition("Error: " + e);
                                }
                        );
                    }, 1000);
                }
   }
                

 //Set audio position on page
   function setAudioPosition(position) {
       $("#audio_position").html(position + " Sek");
   }
   
   /* Navigation of navigationbar in player
   */


   